{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8c69a590-417c-4b2e-a829-e609f2b53a40",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0490c835-8584-4437-974e-2125a086c929",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a9db3e8f-11b8-4731-9ffd-eee693c2d899",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请注意，如果你使用的是课程提供的API，需要额外指定 openai_api_base，比如：\n",
    "# model = ChatOpenAI(model=\"gpt-3.5-turbo\", \n",
    "#                    openai_api_key = \"<你的API密钥>\",\n",
    "#                    openai_api_base = \"https://api.aigc369.com/v1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ebcb3a59-f743-4331-a1b7-6308627fc44e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=1.2, max_tokens=500, model_kwargs={\"frequency_penalty\":1.1})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "726d1ce2-dfe6-4dfa-8c8c-2080caee17ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.schema.messages import HumanMessage, SystemMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "236bacec-2032-4f8e-9132-bc9360ae9c9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages = [\n",
    "    SystemMessage(content=\"请你作为我的物理课助教，用通俗易懂且间接的语言帮我解释物理概念。\"),\n",
    "    HumanMessage(content=\"什么是波粒二象性？\"),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "66502d5d-d529-44a8-b87e-fb1de26d7316",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = model.invoke(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "25bc684c-3cac-4caa-ac06-f8c332b71eb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='嗨！波粒二象性是一个非常有趣的物理概念。它指的是，微观粒子（比如电子、光子等）既可以表现出波动性，也可以表现出粒子性。\\n\\n让我们以光子为例来说明。光子是光的基本粒子，而光又是一种电磁波。当我们将光通过一个狭缝时，它会呈现出波动的特性，产生干涉和衍射现象，就像波一样。但当我们观察光子通过一个光敏探测器时，我们会发现它们的行为更像是粒子，因为它们只在一个点上被探测到。\\n\\n这就是波粒二象性的精髓所在：微观粒子既可以像波一样传播，又可以像粒子一样被探测到。这种二象性挑战了我们对物质本质的传统观念，但也为我们理解微观世界的奇妙规律提供了新的思路。')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5f5f9964-eb1c-4db5-b8a0-8a0b7bd44860",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "嗨！波粒二象性是一个非常有趣的物理概念。它指的是，微观粒子（比如电子、光子等）既可以表现出波动性，也可以表现出粒子性。\n",
      "\n",
      "让我们以光子为例来说明。光子是光的基本粒子，而光又是一种电磁波。当我们将光通过一个狭缝时，它会呈现出波动的特性，产生干涉和衍射现象，就像波一样。但当我们观察光子通过一个光敏探测器时，我们会发现它们的行为更像是粒子，因为它们只在一个点上被探测到。\n",
      "\n",
      "这就是波粒二象性的精髓所在：微观粒子既可以像波一样传播，又可以像粒子一样被探测到。这种二象性挑战了我们对物质本质的传统观念，但也为我们理解微观世界的奇妙规律提供了新的思路。\n"
     ]
    }
   ],
   "source": [
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30577125-ba5b-412f-aa90-14d93f5f3ab4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
